package com.tenglong.teacher.ui.utils;

import android.util.Log;

import com.google.gson.Gson;
import com.tenglong.teacher.services.FastJsonConverterFactory;
import com.tenglong.teacher.services.SMSApi;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * @author hdx on 2019/4/19.
 */
public class MySMSUtils {
    //发送验证码的请求路径URL
    private static final String
            SERVER_URL="https://api.netease.im/";
    //网易云信分配的账号，请替换你在管理后台应用下申请的Appkey
    private static final String
            APP_KEY="098359ca399e251d1656267669106427";
    //网易云信分配的密钥，请替换你在管理后台应用下申请的appSecret
    private static final String APP_SECRET="1119c195d151";
    //随机数
    private static final String NONCE="123456";
    //短信模板ID
    private static final String TEMPLATEID="9574696";
    //验证码长度，范围4～10，默认为4
    private static final String CODELEN="6";
    public static void getVerifyCode(String phone){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(SERVER_URL)
                .addConverterFactory(FastJsonConverterFactory.create())
                .build();
        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        /*
         * 参考计算CheckSum的java代码，在上述文档的参数列表中，有CheckSum的计算文档示例
         */
        String checkSum = CheckSumBuilder.getCheckSum(APP_SECRET, NONCE, curTime);
        Map<String,String> headers = new HashMap<>();
        headers.put("AppKey", APP_KEY);
        headers.put("Nonce", NONCE);
        headers.put("CurTime", curTime);
        headers.put("CheckSum", checkSum);

        Map<String,String> params = new HashMap<>();
        params.put("templateid", TEMPLATEID);
        params.put("mobile", phone);
        params.put("codeLen", CODELEN);
        retrofit.create(SMSApi.class).getVerifyCode("sms/sendcode.action",headers,params).enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
            }
        });
    }
}
